<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\XcxService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class XcxController extends BaseController
{
    //小程序菜单

    //列表
    public function index(Request $request)
    {
        $type = $request->post('type', 0);
        $res = XcxService::index($type);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['link'] = $request->post('link', '');
        $pas['type'] = $request->post('type', 1);
        $icon0 = $request->post('img1', '');
        $icon1 = $request->post('img2', '');
        if ($icon0) {
            if (substr($icon0, 0, 5) == 'data:') {
                $pas['icon0'] = Upload::getPathByBase64($icon0);
            } else {
                $pas['icon0'] = $icon0;
            }
        }
        if ($icon1) {
            if (substr($icon1, 0, 5) == 'data:') {
                $pas['icon1'] = Upload::getPathByBase64($icon1);
            } else {
                $pas['icon1'] = $icon1;
            }
        }
        $res = XcxService::modify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

//    //删除
//    public function del(Request $request)
//    {
//        $id = $request->post('id', 0);
//        $res = CateService::del($id);
//        if ($res['code'] < 0) {
//            return $this->toJson(-2, $res['msg']);
//        }
//        return $this->toJson();
//    }

    //配置获取
    public function configOne(Request $request)
    {
        $res = XcxService::configOne();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //配置设置
    public function configModify(Request $request)
    {
        $res = XcxService::configModify();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}